﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class pa_Warranty
    {
    	#region public properties
		[Required(ErrorMessage="WarrantyID Required")]
		public int WarrantyID { get; set; }
		public string AgencyCode { get; set; }
		public string CustomerPhone { get; set; }
		public string ModelCode { get; set; }
		public string ProductCode { get; set; }
		public DateTime? RegisterDate { get; set; }
		#endregion

		#region constructor
		public pa_Warranty(){ }

		public pa_Warranty(IDataReader rd)
		{
			this.WarrantyID = (int)rd["WarrantyID"];
			this.AgencyCode = rd["AgencyCode"].ToString();
			this.CustomerPhone = rd["CustomerPhone"].ToString();
			this.ModelCode = rd["ModelCode"].ToString();
			this.ProductCode = rd["ProductCode"].ToString();
			this.RegisterDate = rd["RegisterDate"].Equals(DBNull.Value) ? (DateTime?)null : (DateTime)rd["RegisterDate"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBpa_Warranty.Create(AgencyCode, CustomerPhone, ModelCode, ProductCode, RegisterDate);
        }

        private bool Update()
        {
            return DBpa_Warranty.Update(WarrantyID, AgencyCode, CustomerPhone, ModelCode, ProductCode, RegisterDate);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(WarrantyID != 0)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(int WarrantyID)
        {
            return DBpa_Warranty.Delete(WarrantyID);
        }

        public static List<pa_Warranty> GetAll()
        {
            return ObjectClass.CreateList<pa_Warranty>(DBpa_Warranty.GetAll());
        }

        public static pa_Warranty GetByKey(int WarrantyID)
        {
            return ObjectClass.CreateInstance<pa_Warranty>(DBpa_Warranty.GetByKey(WarrantyID));
        }

        public static List<pa_Warranty> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<pa_Warranty>(DBpa_Warranty.GetByTop(top, where, order));
        }
		#endregion
	}
}
